package com.info.search;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Http {

	public static void main(String args[]) {

		// NewsRequest request = new NewsRequest();
		// ArrayList<NewsDAO> list = request.getNewsList();
		// for (int i = 0; i < list.size(); i++) {
		//
		// System.out.println(list.get(i).name);
		//
		// }
		// if (true)
		// return;
		// String header = "http://news.xaufe.edu.cn";
		// String url = "http://news.xaufe.edu.cn/GetNews.asp";
		// Pattern p = Pattern
		// .compile("<a href=(.*?) target=_blank title=(.*?)\"/");
		// try {
		// Matcher m = p.matcher(get(url));
		// while (m.find()) {
		// System.out.println(header
		// + m.group(1).substring(1, m.group(1).length() - 1));
		// System.out.println(m.group(2).substring(1));
		// }
		// } catch (MalformedURLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}

	static String getUrl(String url) throws MalformedURLException, IOException {
		HttpURLConnection conn = (HttpURLConnection) new URL(url)
				.openConnection();
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		conn.setRequestProperty("Referer",
				"http://218.195.32.11/(yh0kxoaa40kpjamm4u0myp55)/xs_main.aspx?xh=1205500121");
		String s;
		System.out.println(conn.getResponseCode());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}
		return conn.getURL().toString();

	}

	static String get(String url) throws MalformedURLException,
			IOException {

		HttpURLConnection conn = (HttpURLConnection) new URL(url)
				.openConnection();
		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		// conn.setRequestProperty("Referer",
		// "http://218.195.32.11/(yh0kxoaa40kpjamm4u0myp55)/xs_main.aspx?xh=1205500121");
		String s;
		System.out.println(conn.getResponseCode());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}

		return b.toString();

	}

	static String get() throws IOException {
		String name = "G:\\Users\\hello\\workspaceteskfcfile\\.metadata\\x.txt";
		name = "F://xxshuo//xx";
		FileInputStream file = new FileInputStream(name);
		return StreamUtils.Stream2String(file);
	}

	static String post(String url, String p) throws MalformedURLException,
			IOException {

		URLConnection conn = new URL(url).openConnection();
		String s;
		conn.setRequestProperty("Origin","http://218.195.32.11");
		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
 
		// conn.setRequestProperty(
		// "Referer",
		// "http://218.195.32.11/(icq1h545n1xvg545qtz4fwro)/xxjsjy.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121611");
		conn.setDoInput(true);
		conn.setDoOutput(true);
		
		if (p != null)
			conn.getOutputStream().write(p.getBytes());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}

		return b.toString();

	}
}
